package com.pzhu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pzhu.model.entity.Specs;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.service.SpecsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @Author : zqh
 * @Date : 2024/1/3 0003 下午 4:05
 * @Version: 1.0
 */
@Api(tags = "规格接口")
@RestController
@RequestMapping("/specs")
public class SpecsController {

    @Autowired
    private SpecsService specsService;

    @ApiOperation("查询套餐规格")
    @GetMapping("/list/{id}")
    @ApiImplicitParam(name = "id",value = "套餐ID",required = true)
    public ResponseResult list(@PathVariable(value = "id")  Long id){
        LambdaQueryWrapper<Specs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Specs::getThaliId,id);
        List<Specs> specsList = specsService.list(queryWrapper);
        return ResponseResult.ok().put("specsList",specsList);
    }

    /**
     * 添加或者修改
     * @param specs
     * @return
     */
    @ApiOperation("添加或修改规格信息")
    @PostMapping("/save")
//    @PreAuthorize("hasAuthority('system:menu:add')"+"||"+"hasAuthority('system:menu:edit')")
    public ResponseResult save(@RequestBody @Valid Specs specs){
        if(specs.getId()==null || specs.getId()==-1){
            specs.setCreateTime(new Date());
            specsService.save(specs);
        }else{
            specs.setUpdateTime(new Date());
            specsService.updateById(specs);
        }
        return ResponseResult.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation("根据id删除规格信息")
    @GetMapping("/delete/{id}")
//    @PreAuthorize("hasAuthority('system:menu:delete')")
    @ApiImplicitParam(name = "id",value = "规格ID",required = true)
    public ResponseResult delete(@PathVariable(value = "id")Long id){
        specsService.removeById(id);
        return ResponseResult.ok();
    }
}
